package com.spudtech.graphics
{
    
    import flash.geom.*;
    import flash.display.*;
    
    import flash.utils.Dictionary;
    
    import com.spudtech.*;
    import com.spudtech.tween.*;
    
    
    public class SpudAnimBody extends SpudAnimBone
    {
        
        protected var _rootBone : SpudAnimBone;
        
        protected var _animations : Dictionary;
        
        protected var _currentAnim : SpudAnimation;
        
        
        public function SpudAnimBody(body_name : String,
                                     body_position : Point,
                                     root_bone : SpudAnimBone,
                                     init_anims : Dictionary)
        {
            if (body_position == null)
            {
                body_position = new Point(0,0);
            }
            
            super(body_name,body_position,null);
            
            _animations = init_anims;
            
            _currentAnim = null;
            
        }
        
        
        public function get rootBone() : SpudAnimBone
        {
            return _rootBone;
        }
        public function set rootBone(root_bone : SpudAnimBone) : void
        {
            if (_rootBone != null)
            {
                // We already have a root bone
                // Detach it
                
                _rootBone.destroy();
                
                removeChild(_rootBone);
                
            }
            
            _rootBone = root_bone;
            
            if (_rootBone != null)
            {
                // We have a new root bone
                // Attach it
                
                addChild(_rootBone);
                
                _rootBone.begin();
                
            }
            
        }
        
        public function get animations() : Dictionary
        {
            return _animations;
        }
        
        
        public function addAnimation(name : String, anim : SpudAnimation) : void
        {
            if (animations[name] != null)
            {
                throw new ArgumentError("Animation named "+name+" already present");
            }
            
            animations[name] = anim;
            
        }
        public function removeAnimation(name : String) : void
        {
            if (animations[name] == null)
            {
                throw new ArgumentError("No animation named "+name);
            }
            
            animations[name] = null;
            
        }
        
        public function get currentAnim() : SpudAnimation
        {
            return _currentAnim;
        }
        
        public function get currentAnimName() : String
        {
            // TODO
            return "";
        }
        
        public function playAnim(name : String, force_reset : Boolean = false) : void
        {
            if (animations[name] == null)
            {
                throw new ArgumentError("No animation named "+name);
            }
            
            if (!force_reset && animations[name] == currentAnim)
            {
                // We don't want to reset if
                // we are trying to play the
                // same animation,
                // and we are indeed trying
                // to play the same animation
                // We just return
                return;
            }
            
            // TODO - The rest of this
            
        }
        
        
        override public function update() : void
        {
            super.update();
            
            
            
        }
        
    }
    
}
